﻿//---------------------------------------------------------------------------------------------------------------------
// 
// The original Photo Gallery Wall application was created by Jeffrey Paries.
// It was written using Microsoft Silverlight 1.0 and JavaScript, but works for Silverlight 2. 
// The source code is available for download at http://designwithsilverlight.com/2008/01/31/photo-gallery-wall/
// The live demo is available at http://www.designwithsilverlight.com/tutorials/photoGalleryWall/Default.html
// 
// The C# version of this application for Silverlight 2 is completed by Zhiming Xue.
// The main work involves converting the javascript code to C# code, and therefore the visual designs are unchanged. 
// Any comments? Please contact Zhiming at his blog, http://blogs.msdn.com/zxue.
//
// Change History
// 3/12/09 Use the WebClient method to download images individually
//         This approach reduces the xap file size since image files are not included in it
//---------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Media.Imaging;
using System.Windows.Browser;

namespace PhotoGalleryWallCSharp
{
    public partial class Page : UserControl
    {
        string displayImageName = "";
        //int imageNumber = 0;
        int nextImageToDisplay = 0;
        int fadeOutCompleted; //event token
        // set up an array for the images
        string[] photoArray = new string[16];
        // set up an array for the images
        string[] photoLabels = new string[16];

        public Page()
        {
            InitializeComponent();
        }

        private void rootCanvas_Loaded(object sender, RoutedEventArgs e)
        {
            //set photo file names and captions
            string galleryTitle = "Photo Gallery";


            photoArray[0] = "images/eyes640x480.jpg";
            photoArray[1] = "images/crescentGlacier640x480.jpg";
            photoArray[2] = "images/adams640x480.jpg";
            photoArray[3] = "images/adamsSunrise640x480.jpg";
            photoArray[4] = "images/msh640x480.jpg";
            photoArray[5] = "images/fireworks640x480.jpg";
            photoArray[6] = "images/cityNight640x480.jpg";
            photoArray[7] = "images/beeFlower640x480.jpg";
            photoArray[8] = "images/softWater640x480.jpg";
            photoArray[9] = "images/columbiaRiver640x480.jpg";
            photoArray[10] = "images/pacific640x480.jpg";
            photoArray[11] = "images/crownPoint640x480.jpg";
            photoArray[12] = "images/yellowFlower640x480.jpg";
            photoArray[13] = "images/purpleFlower640x480.jpg";
            photoArray[14] = "images/yellowRose640x480.jpg";
            photoArray[15] = "images/marigold640x480.jpg";


            photoLabels[0] = "My eyes are an ocean in which my dreams are reflected.";
            photoLabels[1] = "Crescent Glacier, Mt. Adams - June 14th, 2007";
            photoLabels[2] = "";
            photoLabels[3] = "Sunrise over Mt. Adams (from Mt. St. Helens)";
            photoLabels[4] = "";
            photoLabels[5] = "";
            photoLabels[6] = "";
            photoLabels[7] = "";
            photoLabels[8] = "";
            photoLabels[9] = "The Columbia River";
            photoLabels[10] = "Pacific Ocean, Oregon Coast";
            photoLabels[11] = "Crown Point / Vista House";
            photoLabels[12] = "";
            photoLabels[13] = "";
            photoLabels[14] = "";
            photoLabels[15] = "";


            // load the images from the array into
            // the gallery canvases

            displayImageName = "imageBrush00";

            for (int i = 0; i < photoArray.Length; i++)
            {
                string imageName;
                if (i < 10)
                {
                    imageName = "imageBrush0" + i.ToString();
                }
                else
                {
                    imageName = "imageBrush" + i.ToString();
                }
                //sender.findName(imageName).ImageSource = photoArray[i];

                //BitmapImage bmi = new BitmapImage(new Uri(photoArray[i], UriKind.Relative));
                //((ImageBrush)rootCanvas.FindName(imageName)).ImageSource = bmi;
                
                WebClient downloader = new WebClient();
                downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
                downloader.OpenReadAsync(new Uri(photoArray[i], UriKind.Relative), imageName);

            }

            for (int i = 8; i < photoArray.Length; i++)
            {
                string imageName;
                if (i < 10)
                {
                    imageName = "reflectionBrush0" + i.ToString();
                }
                else
                {
                    imageName = "reflectionBrush" + i.ToString();
                }
                //sender.findName(imageName).ImageSource = photoArray[i];
                //BitmapImage bmi = new BitmapImage(new Uri(photoArray[i], UriKind.Relative));
                //((ImageBrush)rootCanvas.FindName(imageName)).ImageSource = bmi;
                WebClient downloader = new WebClient();
                downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
                downloader.OpenReadAsync(new Uri(photoArray[i], UriKind.Relative), imageName);

            }

            //sender.findName("displayImage").ImageSource = photoArray[0];
            //sender.findName("displayImagePaneReflectionImage").ImageSource = photoArray[0];

            //modified to use the WebClient method

            //displayImage.ImageSource = new BitmapImage(new Uri(photoArray[0], UriKind.Relative));
            //displayImagePaneReflectionImage.ImageSource = new BitmapImage(new Uri(photoArray[0], UriKind.Relative));

            WebClient downloader2 = new WebClient();
            downloader2.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
            downloader2.OpenReadAsync(new Uri(photoArray[0], UriKind.Relative), "displayImage");

            WebClient downloader3 = new WebClient();
            downloader3.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
            downloader3.OpenReadAsync(new Uri(photoArray[0], UriKind.Relative), "displayImagePaneReflectionImage");

            //sender.findName("galleryTitleText").text = galleryTitle;
            //sender.findName("labelText").text = photoLabels[0];
            //sender.findName("labelTextShadow").text = photoLabels[0];
            //sender.findName("labelTextReflection").text = photoLabels[0];
            //sender.findName("labelTextShadowReflection").text = photoLabels[0];
            galleryTitleText.Text = galleryTitle;
            labelText.Text = photoLabels[0];
            labelTextShadow.Text = photoLabels[0];
            labelTextReflection.Text = photoLabels[0];
            labelTextShadowReflection.Text = photoLabels[0];


            //var textCenter = sender.findName("rightWall").width / 2;
            //var labelCenter = textCenter - (sender.findName("labelText").ActualWidth / 2);
            double textCenter = rightWall.Width / 2;
            double labelCenter = textCenter - (labelText.ActualWidth / 2);


            //sender.findName("labelText")["Canvas.Left"] = labelCenter;
            //sender.findName("labelTextShadow")["Canvas.Left"] = labelCenter;
            //sender.findName("labelTextReflection")["Canvas.Left"] = labelCenter;
            //sender.findName("labelTextShadowReflection")["Canvas.Left"] = labelCenter;

            labelText.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextShadow.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextReflection.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextShadowReflection.SetValue(Canvas.LeftProperty, labelCenter);


            //exampleControl.addEventListener(element, eventName, handler);
            //The element that exposes the event. 
            //A string that represents the name of the event. This must be a valid event name for the type of element provided.
            //The function to add as the event handler. 

            //object.RemoveEventListener(eventName, token) 
            //object.RemoveEventListener(eventName, functionName)
            //textBlock.removeEventListener("MouseEnter", entertoken1);

            //sender.findName("timer").begin();
            //sender.findName("timer").addEventListener("completed", doSlideshow);
            timer.Begin();
            timer.Completed += doSlideshow;

        }

        void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            // 
            // Create a new BitmapImage and load the stream 
            // 
            BitmapImage loadedImage = new BitmapImage();
            loadedImage.SetSource(e.Result);
            ((ImageBrush)rootCanvas.FindName(e.UserState.ToString())).ImageSource = loadedImage;

        }

        void doSlideshow(object sender, EventArgs args)
        {
            //int imageNumber = 0;
            //// find out which image is currently being displayed
            //string objectName = ((BitmapImage)displayImage.ImageSource).UriSource.ToString();
            //for (int i = 0; i < photoArray.Length; i++)
            //{
            //    if (objectName == photoArray[i])
            //    {
            //        imageNumber = i;
            //        break;
            //    }
            //}

            //nextImageToDisplay = imageNumber + 1;
            nextImageToDisplay = nextImageToDisplay + 1;

            displayImages(sender, args);
            timer.Begin();
        }

        void displayImages(object sender, EventArgs args)
        {
            //sender.findName("fadeOutImage").begin();
            //fadeOutCompleted = sender.findName("fadeOutImage").addEventListener("completed", nextSlide);

            fadeOutImage.Begin();
            //fadeOutCompleted += nextSlide;
            fadeOutImage.Completed += nextSlide;
            //fadeOutImage.Completed(nextSlide);
        }


        void nextSlide(object sender, EventArgs args)
        {
            fadeOutImage.Completed -= nextSlide;

            if (nextImageToDisplay == (photoArray.Length))
            {
                nextImageToDisplay = 0;
            }
            string imageName = "";
            if (nextImageToDisplay < 10)
            {
                imageName = "imageBrush0" + nextImageToDisplay;
            }
            else
            {
                imageName = "imageBrush" + nextImageToDisplay;
            }

           

            labelText.Text = photoLabels[nextImageToDisplay];
            labelTextShadow.Text = photoLabels[nextImageToDisplay];
            labelTextReflection.Text = photoLabels[nextImageToDisplay];
            labelTextShadowReflection.Text = photoLabels[nextImageToDisplay];
            double textCenter = rightWall.Width / 2;
            double labelCenter = textCenter - (labelText.ActualWidth / 2);

            labelText.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextShadow.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextReflection.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextShadowReflection.SetValue(Canvas.LeftProperty, labelCenter);


            displayImage.ImageSource = ((ImageBrush)rootCanvas.FindName(imageName)).ImageSource;
            displayImagePaneReflectionImage.ImageSource = ((ImageBrush)rootCanvas.FindName(imageName)).ImageSource;
            fadeInImage.Begin();
        }

        void showNextImage(object sender, EventArgs args)
        {
            if (nextImageToDisplay == (photoArray.Length))
            {
                nextImageToDisplay = 0;
            }

            int imageNumber = nextImageToDisplay;

            labelText.Text = photoLabels[imageNumber];
            labelTextShadow.Text = photoLabels[imageNumber];
            labelTextReflection.Text = photoLabels[imageNumber];
            labelTextShadowReflection.Text = photoLabels[imageNumber];
            double textCenter = rightWall.Width / 2;
            double labelCenter = textCenter - (labelText.ActualWidth / 2);

            labelText.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextShadow.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextReflection.SetValue(Canvas.LeftProperty, labelCenter);
            labelTextShadowReflection.SetValue(Canvas.LeftProperty, labelCenter);

            displayImage.ImageSource = ((ImageBrush)rootCanvas.FindName(displayImageName)).ImageSource;
            displayImagePaneReflectionImage.ImageSource = ((ImageBrush)rootCanvas.FindName(displayImageName)).ImageSource;
            fadeInImage.Begin();
            timer.Begin();
        }



        void DisplayImageOnMouseLeftButtonDown(object sender, EventArgs args)
        {
            timer.Stop();
            Path p = (Path)sender;
            //string objectName = ((BitmapImage)((ImageBrush)p.Fill).ImageSource).UriSource.ToString();
            string objectName = p.Name;
            string strImageSuffix = objectName.Substring(5, 2);
            int imageNumber = int.Parse(strImageSuffix);
            displayImageName = "imageBrush" + strImageSuffix;
            nextImageToDisplay = imageNumber;
            fadeOutImage.Begin();
            fadeOutImage.Completed += showNextImage;
        }

        void ShowOutlineOnMouseEnter(object sender, EventArgs args)
        {
            Path p = (Path)sender;
            p.StrokeThickness = 2;

            p.Stroke = new SolidColorBrush(Colors.Red);
            //(alpha, red, green, and blue) values. 
            //p.Stroke = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

            //string outlineObjectName = sender.ToString();
            //int outlineImageNumber = int.Parse(outlineObjectName.Substring(5, outlineObjectName.Length));
            string objectName = p.Name;
            string strImageSuffix = objectName.Substring(5, 2);
            int outlineImageNumber = int.Parse(strImageSuffix);

            //change reflection properties for the images at two bottom rows
            if (outlineImageNumber >= 8)
            {
                string reflection = "reflectionImage" + strImageSuffix;
                Path pf = ((Path)rootCanvas.FindName(reflection));
                pf.StrokeThickness = 2;
                //pf.stroke = "#000000";
                pf.Stroke = new SolidColorBrush(Colors.Red);
                //pf.Stroke = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

            }
        }

        void HideOutlineOnMouseLeave(object sender, EventArgs args)
        {
            Path p = (Path)sender;
            p.StrokeThickness = 0;
            //sender.strokeThickness = 0;

            string objectName = p.Name;
            string strImageSuffix = objectName.Substring(5, 2);
            int outlineImageNumber = int.Parse(strImageSuffix);

            //outlineObjectName = sender.name;
            //outlineImageNumber = outlineObjectName.substring(5, outlineObjectName.length);

            if (outlineImageNumber >= 8)
            {
                string reflection = "reflectionImage" + strImageSuffix;
                //sender.findName(reflection).strokeThickness = 0;
                Path pf = ((Path)rootCanvas.FindName(reflection));
                pf.StrokeThickness = 0;

            }
        }


    }
}
